<?php
/**
 * author      :chenwenwen
 * createTime  :2020-05-02 16:20
 * description :
 */

namespace app\api\controller;

use app\common\business\User as UserBusiness;

class User extends AuthBase {
    public function index() {
        $user = (new UserBusiness())->getNormalUserById($this->userId);
        return show(config('status.success'), 'success', $user);
    }

    public function update() {
        $username = input('username', '', 'trim');
        $sex = input('sex', 0, 'intval');

        $data = [
            'username' => $username,
            'sex' => $sex,
        ];
        $validate = (new \app\api\validate\User())->scene('update_user');
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }

        $userBusinessObj = new UserBusiness();
        $user = $userBusinessObj->update($this->userId, $data);
        if (!$user) return show(config('status.error'), '更新失败');

        //更新redis数据
//        $userInfo = cache(config('redis.token_pre').$this->accessToken);
//        $userInfo['username'] = $data['username'];
//        cache(config('redis.token_pre').$this->accessToken,$userInfo);

        return show(config('status.success'), 'success');

    }
}